package com.kfc.platform.web;
import java.io.IOException;
import java.net.BindException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kfc.platform.user.domain.InsertUser;
import com.kfc.platform.user.domain.Teacher;
import com.kfc.platform.user.domain.User;
import com.kfc.platform.user.service.TeacherService;
import com.kfc.platform.user.service.UserService;
@Controller
@RequestMapping(value="/jsp")
public class UserController{
	@Autowired
	private UserService userService;
	@RequestMapping(value="/get")
	@ResponseBody
  	public ModelAndView users(@Valid InsertUser insertuser,HttpServletRequest req,
  			HttpServletResponse response){
		
		User user = new User();
		System.out.println("name:"+insertuser.getUsername());
		System.out.println("pass:"+insertuser.getPassword());
		System.out.println("pass2:"+insertuser.getPassword2());
		System.out.println("phonenum:"+insertuser.getPhonenum());
		System.out.println("email:"+insertuser.getEmail());
		user.setDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		user.setEmail(insertuser.getEmail());
		user.setUsername(insertuser.getUsername());
		user.setPassword(insertuser.getPassword());
		user.setPhonenum(insertuser.getPhonenum());
		System.out.println(userService.createUser(user));
		req.setAttribute("phone", user.getPhonenum());
		req.setAttribute("email", user.getEmail());
		return new ModelAndView("/welcome","name",user.getUsername());
	}
}